package com.weisy.signin.ui.fragment;

import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.squareup.picasso.Picasso;
import com.weisy.signin.R;
import com.weisy.signin.adapter.CommAdapter;
import com.weisy.signin.base.BaseFragment;
import com.weisy.signin.bean.Student;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.utils.ImageUtils;
import com.weisy.signin.widget.CircleTransform;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author weisy
 * @function 成绩管理
 * @date 2019/3/21 21:22
 */
public class ManageScoreFragment extends BaseFragment {
    @BindView(R.id.list_student)
    ListView lisStudent;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.line_no_data)
    LinearLayout lineNull;

    private CommAdapter<Student> adapter;
    private ArrayList<Student> students = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.frag_manage_score;
    }

    @Override
    public void initViews(View view) {

        adapter = new CommAdapter<Student>(students, R.layout.layout_item_mage_stu) {
            @Override
            public void bindView(ViewHolder holder, Student obj) {
                if (!TextUtils.isEmpty(obj.getImgUrl())) {
                    Bitmap bitmap = ImageUtils.str2Bitmap(obj.getImgUrl());
                    Picasso.get().load(ImageUtils.bit2Uri(context, bitmap)).error(R.drawable.ic_head_default)
                            .placeholder(R.drawable.ic_head_default).transform(new CircleTransform()).into((ImageView) holder.getView(R.id.img_stu_head));
                }
                holder.setText(R.id.txt_stu_item1, obj.getName());
                holder.setText(R.id.txt_stu_item2, obj.getStudentId());
                holder.setText(R.id.txt_stu_item3, obj.getMajor());
            }
        };
        lisStudent.setAdapter(adapter);
        lisStudent.setOnItemClickListener((parent, view1, position, id) -> {
            ARouter.getInstance().build(Comm.ROUTE_ADD_SCORE)
                    .withParcelable("student", students.get(position))
                    .navigation();
        });
        refreshLayout.setOnRefreshListener(refreshLayout -> onResume());
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void onResume() {
        super.onResume();
        students.clear();
        List<Student> tmp = LitePal.findAll(Student.class);
        if (null != tmp && !tmp.isEmpty()) {
            students.addAll(tmp);
            adapter.notifyDataSetChanged();
        }
        if (students.isEmpty()){
            lineNull.setVisibility(View.VISIBLE);
        }else {
            lineNull.setVisibility(View.GONE);
        }
        new Handler().postDelayed(() -> refreshLayout.finishRefresh(), 1000);
    }
}
